package io.renren.modules.frt.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.toolkit.StringUtils;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.frt.entity.DistrictEntity;
import io.renren.modules.frt.entity.EconomicZoneEntity;
import io.renren.modules.frt.service.DistrictService;
import io.renren.modules.frt.service.EconomicZoneService;



/**
 * 三大经济地带管理
 *
 * @author qibing.huang
 * @email abingtech@163.com
 * @date 2018-11-17 19:37:31
 */
@RestController
@RequestMapping("frt/economiczone")
public class EconomicZoneController {
    @Autowired
    private EconomicZoneService economicZoneService;
    @Autowired
    private DistrictService districtService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("frt:economiczone:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = economicZoneService.queryPage(params);
        
        List<EconomicZoneEntity> list = (List<EconomicZoneEntity>) page.getList();
		if (list != null && list.size() > 0) {
			for (EconomicZoneEntity ec : list) {
				if (StringUtils.isNotEmpty(ec.getProvince())) {
					DistrictEntity province = districtService.selectOne(new EntityWrapper<DistrictEntity>().eq("code", ec.getProvince()));
					ec.setProvinceName(province != null ? province.getName() : "");
				}
			}
		}

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("frt:economiczone:info")
    public R info(@PathVariable("id") Long id){
        EconomicZoneEntity economicZone = economicZoneService.selectById(id);

        return R.ok().put("economiczone", economicZone);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("frt:economiczone:save")
    public R save(@RequestBody EconomicZoneEntity economicZone){
		economicZoneService.insert(economicZone);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("frt:economiczone:update")
    public R update(@RequestBody EconomicZoneEntity economicZone){
		economicZoneService.updateById(economicZone);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("frt:economiczone:delete")
    public R delete(@RequestBody Long[] ids){
		economicZoneService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    
	@GetMapping("/select")
	public R select(){
		List<EconomicZoneEntity> list = economicZoneService.selectList(new EntityWrapper<EconomicZoneEntity>()
				.setSqlSelect("zone_name").groupBy("zone_name"));
		return R.ok().put("list", list);
	}

}
